﻿'-------------------------------------------------------------------------------
'Developer: Ivan Tamurejo
'-------------------------------------------------------------------------------
'This module includes functions to clean, read and write files logs.
'-------------------------------------------------------------------------------
Imports System.IO
Imports System.Text

Public Module Logs

#Region "Enumerated types"

    Public Enum eTypeLog
        Builds
        Robot
    End Enum

#End Region

#Region "Private variables"

    Private BUILDS_LOG As String
    Private ROBOT_LOG As String

#End Region

#Region "Cleaning, read and write to logs operations"

    Public Sub CleanLog(ByVal sBuildsDirectory As String, ByVal elog As eTypeLog)

        Select Case elog

            Case eTypeLog.Robot

                If ROBOT_LOG = "" Then
                    Dim CurrentDate As Date = Now
                    ROBOT_LOG = Path.Combine(sBuildsDirectory, "Logs")
                    ROBOT_LOG = Path.Combine(ROBOT_LOG, "Robot" & _
                                             DateAndTime.Year(CurrentDate).ToString.PadLeft(4, Chr(48)) & _
                                             DateAndTime.Month(CurrentDate).ToString.PadLeft(2, Chr(48)) & _
                                             DateAndTime.Day(CurrentDate).ToString.PadLeft(2, Chr(48)) & ".log")
                End If

                If File.Exists(ROBOT_LOG) Then
                    Try
                        File.Delete(ROBOT_LOG)
                        ROBOT_LOG = ""
                    Catch
                    End Try
                End If

            Case eTypeLog.Builds

                If BUILDS_LOG = "" Then
                    Dim CurrentDate As Date = Now
                    BUILDS_LOG = Path.Combine(sBuildsDirectory, "Logs")
                    BUILDS_LOG = Path.Combine(BUILDS_LOG, "Builds" & _
                                              DateAndTime.Year(CurrentDate).ToString.PadLeft(4, Chr(48)) & _
                                              DateAndTime.Month(CurrentDate).ToString.PadLeft(2, Chr(48)) & _
                                              DateAndTime.Day(CurrentDate).ToString.PadLeft(2, Chr(48)) & ".log")
                End If

                If File.Exists(BUILDS_LOG) Then
                    Try
                        File.Delete(BUILDS_LOG)
                        BUILDS_LOG = ""
                    Catch
                    End Try
                End If

        End Select

    End Sub

    Public Function ReadLog(ByVal sBuildsDirectory As String, ByVal eLog As eTypeLog) As String

        ReadLog = ""

        Select Case eLog
            Case eTypeLog.Robot

                If ROBOT_LOG = "" Then
                    Dim CurrentDate As Date = Now
                    ROBOT_LOG = Path.Combine(sBuildsDirectory, "Logs")
                    ROBOT_LOG = Path.Combine(ROBOT_LOG, "Robot" & _
                                             DateAndTime.Year(CurrentDate).ToString.PadLeft(4, Chr(48)) & _
                                             DateAndTime.Month(CurrentDate).ToString.PadLeft(2, Chr(48)) & _
                                             DateAndTime.Day(CurrentDate).ToString.PadLeft(2, Chr(48)) & ".log")
                End If

                If File.Exists(ROBOT_LOG) Then
                    ReadLog = Trim(GetAllTextFileEncondingDefault(ROBOT_LOG))
                End If

            Case eTypeLog.Builds

                If BUILDS_LOG = "" Then
                    Dim CurrentDate As Date = Now
                    BUILDS_LOG = Path.Combine(sBuildsDirectory, "Logs")
                    BUILDS_LOG = Path.Combine(BUILDS_LOG, "Builds" & _
                                              DateAndTime.Year(CurrentDate).ToString.PadLeft(4, Chr(48)) & _
                                              DateAndTime.Month(CurrentDate).ToString.PadLeft(2, Chr(48)) & _
                                              DateAndTime.Day(CurrentDate).ToString.PadLeft(2, Chr(48)) & ".log")
                End If

                If File.Exists(BUILDS_LOG) Then

                    ReadLog = Trim(GetAllTextFileEncondingDefault(BUILDS_LOG))

                End If

        End Select

        Return ReadLog

    End Function

    Public Sub WriteLog(ByVal sBuildsDirectory As String, ByVal sMessage As String, ByVal eLog As eTypeLog)

        Select Case eLog
            Case eTypeLog.Robot

                sMessage = "[" & DateAndTime.TimeString & "]" & sMessage

                If ROBOT_LOG = "" Then
                    If Directory.Exists(sBuildsDirectory) Then
                        Dim CurrentDate As Date = Now

                        ROBOT_LOG = Path.Combine(sBuildsDirectory, "Logs")

                        'if not exist directory logs then create. 
                        If Not Directory.Exists(ROBOT_LOG) Then
                            Directory.CreateDirectory(ROBOT_LOG)
                        End If

                        ROBOT_LOG = Path.Combine(ROBOT_LOG, "Robot" & _
                                                  DateAndTime.Year(CurrentDate).ToString.PadLeft(4, Chr(48)) & _
                                                  DateAndTime.Month(CurrentDate).ToString.PadLeft(2, Chr(48)) & _
                                                  DateAndTime.Day(CurrentDate).ToString.PadLeft(2, Chr(48)) & ".log")
                        'DateAndTime.Hour(CurrentDate).ToString.PadLeft(2, Chr(48)) & _
                        'DateAndTime.Minute(CurrentDate).ToString.PadLeft(2, Chr(48)) & _
                        'DateAndTime.Second(CurrentDate).ToString.PadLeft(2, Chr(48)) & ".Log")
                    End If
                End If

                File.AppendAllText(ROBOT_LOG, vbCrLf & sMessage, Encoding.Default)
                Console.WriteLine(sMessage)


            Case eTypeLog.Builds

                If BUILDS_LOG = "" Then
                    If Directory.Exists(sBuildsDirectory) Then
                        Dim CurrentDate As Date = Now

                        BUILDS_LOG = Path.Combine(sBuildsDirectory, "Logs")

                        'if not exist directory logs then create. 
                        If Not Directory.Exists(BUILDS_LOG) Then
                            Directory.CreateDirectory(BUILDS_LOG)
                        End If

                        BUILDS_LOG = Path.Combine(BUILDS_LOG, "Builds" & _
                                                  DateAndTime.Year(CurrentDate).ToString.PadLeft(4, Chr(48)) & _
                                                  DateAndTime.Month(CurrentDate).ToString.PadLeft(2, Chr(48)) & _
                                                  DateAndTime.Day(CurrentDate).ToString.PadLeft(2, Chr(48)) & ".log")
                    End If
                End If

                File.AppendAllText(BUILDS_LOG, vbCrLf & vbCrLf & sMessage, Encoding.Default)

        End Select

    End Sub

#End Region

End Module
